package com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bean.VideoListBean;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.myapp.MyApp;
import com.xiaochun.hxhj.R;

import java.util.List;

/**
 * Created by fei on 2017/12/30.
 */

public class VideoRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    public static final int TYPE_NORMAL = 5;
    public static final int TYPE_BOTTOM = 6;//底部

    private List<VideoListBean.DataBeanX.DataBean> mDatas;
    private Context context;
    private MyApp myApp;

    public VideoRecyclerViewAdapter(List<VideoListBean.DataBeanX.DataBean> mDatas, Context context, MyApp myApp) {
        this.mDatas = mDatas;
        this.context = context;
        this.myApp = myApp;
    }

    private View bottomView;

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener li) {
        mListener = li;
    }


    public void setBottomView(View bottomView) {
        this.bottomView = bottomView;
    }

    public void setDatas(List<VideoListBean.DataBeanX.DataBean> datas) {
        this.mDatas = datas;
        notifyDataSetChanged();
       /* if (datas.size() > 10){
            notifyItemRangeChanged(datas.size()-10,10);
        }else{
            notifyItemRangeChanged(0,datas.size());
        }*/
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mDatas.size()) return TYPE_BOTTOM;
        return TYPE_NORMAL;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (bottomView != null && viewType == TYPE_BOTTOM) {
            return new Holder(bottomView);

        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_videolistre, parent, false);
        return new Holder(layout);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {

        if (getItemViewType(position) == TYPE_BOTTOM) {
            return;
        }
        final int pos = getRealPosition(viewHolder);
            final VideoListBean.DataBeanX.DataBean bean = mDatas.get(pos);
            if (viewHolder instanceof Holder) {

//            GlideUtils.loadRoundCircleImage(context, bean.getImage(), ((Holder) viewHolder).img);

               /* //设置图片圆角角度
                RoundedCorners roundedCorners = new RoundedCorners(10);
                //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
                RequestOptions override = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
                Glide.with(context).load(bean.getImage())
                        .apply(override)
                        .into(((Holder) viewHolder).img);*/


                RoundedCorners roundedCorners = new RoundedCorners(16);//数字为圆角度数
                RequestOptions coverRequestOptions = new RequestOptions()
                        .transforms(new CenterCrop(),roundedCorners)
                        .diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
                        .skipMemoryCache(true);//不做内存缓存
                Glide.with(context)
                        .asBitmap()
                        .load(bean.getImage())
                        .apply(coverRequestOptions)
                        .into(((Holder) viewHolder).img);

            ((Holder) viewHolder).tv_title.setText(bean.getName());
            ((Holder) viewHolder).tv_free.setText(bean.getFree_type_text());
            ((Holder) viewHolder).tv_count.setText(bean.getStudy_num()+"人已学");
                if (mListener != null) {
                    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mListener.onItemClick(pos);
                        }
                    });
                }



            }

    }





    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return position;
    }

    @Override
    public int getItemCount() {
        return mDatas.size() + 1;
    }

    class Holder extends RecyclerView.ViewHolder {

        private ImageView img;//图片
        private TextView tv_title;//名字
        private TextView tv_free;//介绍
        private TextView tv_count;//查看详情

        public Holder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.img_video);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_free = (TextView) itemView.findViewById(R.id.tv_free);
            tv_count = (TextView) itemView.findViewById(R.id.tv_count);

        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }



}